#!/bin/bash

# DEBUG
# set -x

SAMPLEDIR="../../audio_samples"
INFILE="$SAMPLEDIR/sinus_10sec_440hz.wav"
INTYPE1="wm"
OUTTYPE1="wm"
INTYPE2="wm"
OUTTYPE2="wm"
# either encrypted_wav and to_decrypt_wav are the same and we do nothing with nocompress
# or we keep the orignal encrpyted file an copy it to to_decrypt in nocompress
#erich: encrypted.wav is the encrypted file from stegit, to_decrypt is which steg-it gets to decrypt. i wanted to have both for testing the codec conversions. why dont keep both? 
ENCRYPTED_WAV="$SAMPLEDIR/encrypted.wav"
TO_DECRYPT_WAV="$SAMPLEDIR/to_decrypt.wav"
ENCRYPTED_COMPRESSED_MP3="$SAMPLEDIR/encrypted_compressed.mp3"
ENCRYPTED_COMPRESSED_WAV="$SAMPLEDIR/encrypted_compressed.wav"
ENCRYPTED_RAW="$SAMPLEDIR/encrypted.raw"
ENCRYPTED_G729="$SAMPLEDIR/encrypted.g729"
ENCRYPTED_G729_RAW="$SAMPLEDIR/encrypted.g729.raw"
ENCRYPTED_G729_WAV="$SAMPLEDIR/encrypted.g729.wav"
DECRYPTED="$SAMPLEDIR/decrypted.wav"
BINDIR="./io/build-default"
CONFDIR="config"

if [[ $# -lt 1 ]]; then
	echo "Usage: ./testCrypto [-f INFILE] [encryption method] [compress|nocompress|alaw|g729] [stegit params]"
	echo ""
	echo "Encryption methods: a, A, f"
	echo "INFILE must be an absolute or relativ path"
	echo ""
	echo "Example 1: ./testCrypto a compress			# Uses encryption method a and compresses file with mp3 320 kbit"
	echo "Example 2: ./testCrypto a nocompress			# Uses encryption method a and does not compress"
	echo "Example 3: ./testCrypto a alaw				# Uses encryption method a and compresses file with alaw"
	echo "Example 4: ./testCrypto a g729			 	# Uses encryption method a and compresses file with G.729a"
	echo "Example 5: ./testCrypto -f silence.wav a nocompress	# Uses encryption method a, does not compress and uses silence.wav as INFILE"
	echo "Example 6: ./testCrypto -i1 p -o2 p f nocompress # Uses encryption method f, input for encryption from pulse audio and output from decryption also pulse audio" 
	echo ""
	exit 1
fi

if [[ $# -ge 2 && "$1" == "-f" ]]; then
	INFILE="$2"
	shift
	shift
fi

if [[ $# -ge 2 && "$1" == "-i1" ]]; then
	INTYPE1="$2"
	shift
	shift
fi

if [[ $# -ge 2 && "$1" == "-o1" ]]; then
	OUTTYPE1="$2"
	shift
	shift
fi



if [[ $# -ge 2 && "$1" == "-i2" ]]; then
	INTYPE2"$2"
	shift
	shift
fi

if [[ $# -ge 2 && "$1" == "-o2" ]]; then
	OUTTYPE2="$2"
	shift
	shift
fi



if [[ $# -ge 1 ]]; then
	crypt="$1"
	shift
else
	crypt="a"
fi


if [[ $# -ge 1 ]]; then
	codec="$1"
	shift
else
	codec="nocompress"
fi
	
	if [[ ! -f "$INFILE" ]]; then
	echo "input file $INFILE not readable"
	return 1
fi

# Delete files if they exist from previous sessions
for FILE in $ENCRYPTED_WAV $ENCRYPTED_COMPRESSED_MP3 $DECRYPTED $ENCRYPTED_PCM_ALAW $ENCRYPTED_RAW $ENCRYPTED_G729 $ENCRYPTED_G729_WAV $ENCRYPTED_G729_RAW $TO_DECRYPT_WAV $ENCRYPTED_COMPRESSED_WAV ; do
	if [ -f $FILE ]; then
		rm $FILE
	fi
done

#exit 1

echo '!!!!!!!!! start encryption !!!!!!!!!!!!!!'
echo "$BINDIR/stegit-2 -c $CONFDIR/stegit-2.conf -E \"$crypt\" -r \"$INTYPE1\" -w \"$OUTTYPE1\" \"$INFILE\" -2 \"$ENCRYPTED_WAV\""
$BINDIR/stegit-2 -c $CONFDIR/stegit-2.conf -E "$crypt" -r "$INTYPE1" -w "$OUTTYPE1" -1 "$INFILE" -2 "$ENCRYPTED_WAV" $@

if [[ $RC -ne 0 ]]; then
	echo "Error..."
	return 1
fi


if [[ "$INTYPE2" != "no" ]]; then 

		echo "Codec: $codec"
		
		
		if [[ "$OUTTYPE1" == "wm" && "$INTYPE2" == "wm" ]]; then
		
				if [ "$codec" == "compress" ]; then
					echo '!!!!!!!!!!!!!!!!!!!! Compressing the file with mp3 !!!!!!!!!!!!!!!!!!!!!!'
					lame -b 128 "$ENCRYPTED_WAV" "$ENCRYPTED_COMPRESSED_MP3"
					sox "$ENCRYPTED_COMPRESSED_MP3" "$TO_DECRYPT_WAV"
				elif [ "$codec" == "alaw" ]; then
					echo '!!!!!!!!!!!!!!!!!!!! Compressing the file with pcm-a-law !!!!!!!!!!!!!!!!!!!!!!'
					ffmpeg -i "$ENCRYPTED_WAV" -acodec pcm_alaw "$ENCRYPTED_COMPRESSED_WAV"
					ffmpeg -i "$ENCRYPTED_COMPRESSED_WAV" -acodec pcm_s16le "$TO_DECRYPT_WAV"
				elif [ "$codec" == "g729" ]; then
					echo '!!!!!!!!!!!!!!!!!!!! Compressing the file with g729 !!!!!!!!!!!!!!!!!!!!!!'
					sox -r 8k -e signed -b 16 -t raw "$ENCRYPTED_WAV" "$ENCRYPTED_RAW"
					./g729_coder "$ENCRYPTED_RAW" "$ENCRYPTED_G729"
					./g729_decoder "$ENCRYPTED_G729" "$ENCRYPTED_G729_RAW"
					sox -r 8k -e signed -b 16  "$ENCRYPTED_G729_RAW" "$TO_DECRYPT_WAV"
				elif [ "$codec" == "nocompress" ]; then
					# encrypted_wav and to_decrypt_wav are the same??? erich: yes, but only in nocompress, as i want to keep every conversion step from the beginning to the end: input -> steg enc -> [compressed] -> steg dec -> output
					echo "copying encrypted_wav to to_decrypt_wav"
					cp $ENCRYPTED_WAV $TO_DECRYPT_WAV
				fi
		
		fi
		
		echo '!!!!!!!!! start decryption !!!!!!!!!!!!!!'
		echo "$BINDIR/stegit-2 -c $CONFDIR/stegit-2.conf -E \"$crypt\" -R \"$INTYPE2\" -W \"$OUTTYPE2\" -3 \"$TO_DECRYPT_WAV\" -4 \"$DECRYPTED\" $@"
		$BINDIR/stegit-2 -c $CONFDIR/stegit-2.conf -E "$crypt" -R "$INTYPE2" -W "$OUTTYPE2" -3 "$TO_DECRYPT_WAV" -4 "$DECRYPTED" $@
		
		if [[ $RC -ne 0 ]]; then
			echo "Error..."
			return 1
		fi

fi
